/**
 * 
 */
package org.acsis.covoiturage.frontend.actions;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.acsis.covoiturage.frontend.application.SVC;
import org.acsis.covoiturage.services.IClientServices;
import org.acsis.covoiturage.shared.IClient;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.acsis.covoiturage.frontend.forms.*;
/**
 * @author Abdellah
 *
 */
public class InscriptionAction extends Action {

	/* (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		String identifiant= ((InscriptionForm)form).getIdentifiant();
		String motDePasse=((InscriptionForm)form).getMotDePasse();
		String motDePasseConfirmation=((InscriptionForm)form).getMotDePasseConfirmation();
		String civilite=((InscriptionForm)form).getCivilite();
		String nom=((InscriptionForm)form).getNom();
		String prenom=((InscriptionForm)form).getPrenom();
		String email=((InscriptionForm)form).getEmail();
		String dateDeNaissance=((InscriptionForm)form).getDateDeNaissance();
		Date ddn= (new SimpleDateFormat("dd/MM/yyyy")).parse(dateDeNaissance);
		
		if (motDePasse.equals(motDePasseConfirmation)) {
			IClientServices clientSVC = SVC.create(IClientServices.class);
			IClient client= clientSVC.doInscription(civilite, nom, prenom, ddn, email, identifiant, motDePasse);
			if (client!=null) {
				return mapping.findForward("success");

			} else {
				ActionErrors errors = new ActionErrors();
				errors.add("inscriptionEchec",new ActionMessage("erreur.inscriptionEchec"));
				saveErrors(request, errors);
				return mapping.findForward("failed");
			}
		} else {
			ActionErrors errors = new ActionErrors();
			errors.add("motPasseConfirmation",new ActionMessage("erreur.motPasseConfirmation"));
			saveErrors(request, errors);
			return mapping.findForward("failed");
		}
		
		
	}

}
